﻿using DataGrid.Controls;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DataGrid
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Columns.Clear();

            var tmpColumn = new DataGridViewProgressBarColumn();
            tmpColumn.Name = "progressColumn";
            tmpColumn.HeaderText = "下载进度";
            dataGridView1.Columns.Add(tmpColumn);

            var tmpImgColumn = new DataGridViewProgressImageColumn();
            tmpImgColumn.Name = "progressImgColumn";
            tmpImgColumn.HeaderText = "下载进度1";
            dataGridView1.Columns.Add(tmpImgColumn);

            //11
            var chkColumn = new DataGridViewCheckBoxColumn();
            chkColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            chkColumn.HeaderText = "选择";
            chkColumn.FalseValue = false;
            chkColumn.TrueValue = true;
            chkColumn.DefaultCellStyle.NullValue = false;
            chkColumn.ReadOnly = false;
            dataGridView1.Columns.Insert(0, chkColumn);

            var chkCell = new DatagridviewCheckboxHeaderCell();
            chkCell.OnCheckBoxClicked += new DatagridviewcheckboxHeaderEventHander(chkCell_OnCheckBoxClicked);
            var chkBoxCell = dataGridView1.Columns[0] as DataGridViewCheckBoxColumn;
            chkBoxCell.HeaderCell = chkCell;
            chkBoxCell.HeaderCell.Value = string.Empty;

            //this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.DataError += dataGridView1_DataError;
            //this.dataGridView1.CellFormatting += dataGridView1_CellFormatting;
            this.dataGridView1.RowPostPaint += dgv_RowPostPaint;
            DgvRowColor(dataGridView1);

            iProcess = 0;
        }

        private void chkCell_OnCheckBoxClicked(object sender, DatagridviewCheckboxHeaderEventArgs e)
        {
            dataGridView1.EndEdit();

            if (e.CheckedState)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = 1;
                }
                //foreach (DataGridViewRow dgvRow in this.dgv.Rows)
                //{
                //    dgvRow.Cells["dataGridViewCheckBoxColumn1"].Value = true;
                //}
            }
            else
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = 0;
                }
                //foreach (DataGridViewRow dgvRow in this.dgv.Rows)
                //{
                //    dgvRow.Cells["dataGridViewCheckBoxColumn1"].Value = 0;
                //}
            }
        }

        void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            Console.WriteLine(e.Exception.Message);
        }

        void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            if (dgv != null && dgv.Columns[e.ColumnIndex].GetType() == typeof(DataGridViewProgressBarColumn))
            {
                Console.WriteLine(e.Value);
                DataGridViewProgressBarCellStyle dgvcs = new DataGridViewProgressBarCellStyle(Color.YellowGreen, true, e.CellStyle);
                e.CellStyle = dgvcs;
            }
        }

        #region 数据集样式处理
        /// <summary>
        /// 表头序号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //行头显示行号
            var grid = sender as DataGridView;
            var rowidx = (e.RowIndex + 1).ToString();
            var centerFormat = new StringFormat()
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };
            var headerBuunds = new System.Drawing.Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
            e.Graphics.DrawString(rowidx, new Font("宋体", 11), SystemBrushes.ControlText, headerBuunds, centerFormat);
        }

        /// <summary>
        /// 隔行变色
        /// </summary>
        /// <param name="sender"></param>
        private void DgvRowColor(DataGridView sender)
        {
            if (sender.Rows.Count != 0)
            {
                for (int i = 0; i < sender.Rows.Count; i++)
                {
                    if ((i + 1) % 2 == 0)
                    {
                        sender.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.White;
                    }
                    else
                    {
                        sender.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(224, 254, 254);
                    }
                }
            }
        }
        #endregion 数据集样式处理

        int iProcess = 0;

        private void button2_Click(object sender, EventArgs e)
        {
            iProcess += 10;
            //if (dataGridView1.CurrentRow != null)
            //{
            //    dataGridView1.CurrentRow.Cells[0].Value = 10;
            //}

            dataGridView1.Rows[0].Cells[0].Value = iProcess;
            dataGridView1.Rows[0].Cells[1].Value = iProcess;
        }
    }
}
